package com.lyj.io.module.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyj.io.common.utils.DateUtil;
import com.lyj.io.common.utils.PageUtils;
import com.lyj.io.common.utils.Query;
import com.lyj.io.module.sys.dao.SysUserDao;
import com.lyj.io.module.sys.entity.SysPermission;
import com.lyj.io.module.sys.entity.SysUser;
import com.lyj.io.module.sys.service.SysPermissionService;
import com.lyj.io.module.sys.service.SysUserRoleService;
import com.lyj.io.module.sys.service.SysUserService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户接口
 * @author: 李亚杰
 * @version: 1.0
 * @date: 2021/8/19 14:31
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUser> implements SysUserService {

    @Autowired
    SysUserRoleService sysUserRoleService;

    @Autowired
    SysPermissionService sysPermissionService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<SysUser>();
        queryWrapper.eq("delete_flag","0");
        IPage<SysUser> page = this.page(
                new Query<SysUser>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public SysUser queryUserByUsername(String username) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<SysUser>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("delete_flag", "0");
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<String> queryAllMenuId(String userId) {
        //根据用户id查询角色id
        List<String> roleIds = sysUserRoleService.getRoleIdsByUserId(userId);
        //查询每个角色拥有的菜单角色
        String sysPermissions = "";
        for (String role : roleIds) {
            SysPermission sysPermission = sysPermissionService.getPermissionsByRoleId(role);
            if (null != sysPermission) {
                sysPermissions += sysPermission.getPermission();
            }
        }
        if (!"".equals(sysPermissions)) {
            return Arrays.asList(sysPermissions.split(","));
        }
        return null;
    }

    @Override
    public void saveUser(SysUser sysUser) {
        //sha256加密
        String salt = RandomStringUtils.randomAlphanumeric(20);
        sysUser.setPassword(new Sha256Hash(sysUser.getPassword(), salt).toHex());
        sysUser.setSalt(salt);
        this.save(sysUser);
    }

    @Override
    public void removeByIds(List<String> userIds){
        UpdateWrapper<SysUser> updateWrapper = new UpdateWrapper<SysUser>();
        updateWrapper.in("user_id",userIds);
        updateWrapper.set("delete_flag", DateUtil.format2String(DateUtil.YYYYMMDDHHMMSS, new Date()));
        this.baseMapper.update(null, updateWrapper);
    }

    @Override
    public void changeUserStatus(SysUser sysUser) {
        UpdateWrapper<SysUser> updateWrapper = new UpdateWrapper<SysUser>();
        updateWrapper.in("user_id",sysUser.getUserId());
        updateWrapper.set("status", sysUser.getStatus());
        this.baseMapper.update(null, updateWrapper);
    }
}
